﻿using Elight.Entity.Sys;
using Elight.Utility.Core;
using Elight.Utility.Network;
using Elight.WinForm.Common;
using Elight.WinForm.Page;
using Elight.WinForm.Page.Sys.User;
using Elight.WinForm.Page.Sys.Item;
using Elight.WinForm.Page.Sys.Organize;
using Elight.WinForm.Page.Sys.Permission;
using Elight.WinForm.Page.Sys.Role;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Elight.WinForm.Page.Sys.Logs;

namespace Elight.WinForm
{
    /// <summary>
    /// 主界面
    /// </summary>
    public partial class MainForm : UIAsideMainFrame
    {
        public MainForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 画面加载 显示菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            //设置关联
            Aside.TabControl = MainTabControl;
            //获得用户权限
            string url = GlobalConfig.Url + "app/home/getPermission";
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("userId", GlobalConfig.CurrentUser.Id);
            string str = HttpUtils.DoGet(url, parameters, 2000);
            List<SysPermission> list;
            try
            {
                list = str.ToList<SysPermission>();
            }
            catch
            {
                list = null;
            }
            if (list == null)
            {
                this.ShowInfoDialog("网络或服务器异常，请稍后重试", UIStyle.White);
                return;
            }
            GlobalConfig.PermissionList = list;
            //取出一级菜单
            List<SysPermission> root = list.Where(it => it.ParentId == "0").ToList();
            foreach (SysPermission permission in root)
            {
                TreeNode parent = Aside.CreateNode(permission.Name, permission.SymbolIndex, 24, permission.SortCode.Value);
                List<SysPermission> childList = list.Where(it => it.ParentId == permission.Id).ToList();
                foreach (SysPermission child in childList)
                {
                    Aside.CreateChildNode(parent, AddPage(CreateUIPage(child), child.SortCode.Value));
                }
            }
            Aside.SelectFirst();
        }


        /// <summary>
        /// 根据不同的权限，创建不同的Page
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        private UIPage CreateUIPage(SysPermission permission)
        {
            UIPage page;
            if (permission.EnCode=="sys-user") page = new UserPage();
            else if (permission.EnCode=="sys-permission") page = new PermissionPage();
            else if (permission.EnCode=="sys-role") page = new RolePage();
            else if (permission.EnCode=="sys-organize") page = new OrganizePage();
            else if (permission.EnCode=="lay-item") page = new ItemPage();
            else if (permission.EnCode=="sys-log") page = new LogPage();
            else page = new EmptyPage();
            //将菜单名和图标放上去
            page.Text = permission.Name;
            page.Symbol = permission.SymbolIndex;
            return page;
        }

        /// <summary>
        /// 退出提示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ShowAskDialog("您确定要退出吗？", UIStyle.White))
            {
                Environment.Exit(0);
                return;
            }
            e.Cancel = true;
        }


        /// <summary>
        /// 菜单选择
        /// </summary>
        /// <param name="node"></param>
        /// <param name="item"></param>
        /// <param name="pageIndex"></param>
        private void Aside_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {
            GlobalConfig.CurrentMenuText = node.Text;
        }
    }
}
